package com.watf.functions.core;

import java.io.*; 

import org.openqa.selenium.io.FileHandler;

import jxl.*; 
import jxl.write.*;

/*
 * FileName:DefaultFileUtil. Implements the FileUtil interface.
 * This file is used to define the common function used during the file handling. 
 * 
 * If you want to change the file, please update the info bellow after your updating.
 * 
 * Change History:
 * 2011-7-22 First Draft   Guessay.
 */

public class DefaultFileUtil implements FileUtil{
		
	//The method to create a new excel which content isn't number.
	public void createExcel (String excelName, int sheetnumber, String sheetName, int column, int line, String content){		
		try {			
			WritableWorkbook book  =  Workbook.createWorkbook( new  File( excelName + ".xls" ));
			WritableSheet sheet  =  book.createSheet( sheetName ,  sheetnumber - 1 );
			 Label label  =   new  Label( column - 1,  line - 1 ,  content );
			 sheet.addCell(label);
			 book.write();
			 book.close();
		} catch (Exception e){			
			 System.out.println(e);
		}
	}
	
	//The method to create a new excel which content is only number.
	public void createExcel (String excelName, int sheetnumber, String sheetName, int column, int line, double content){		
		try {
			WritableWorkbook book  =  Workbook.createWorkbook( new  File( excelName + ".xls" ));
			WritableSheet sheet  =  book.createSheet( sheetName ,  sheetnumber - 1 );
			jxl.write.Number number  =   new  jxl.write.Number( column -1 ,  line -1 ,  content );
			sheet.addCell(number);
			book.write();
			book.close();
		} catch (Exception e){			
			 System.out.println(e);
		}
	}	
	
	// The method to read the content of an excel.
	public String readExcel (String excelName, int sheetnumber, int column, int line) throws Exception{
			Workbook book  =  Workbook.getWorkbook( new  File( excelName + ".xls" ));
			Sheet sheet  =  book.getSheet( sheetnumber - 1 );
			Cell cell1  =  sheet.getCell( column - 1 ,  line - 1 );
			String result  =  cell1.getContents();
            book.close();
            return result;
	}
	
	// The method to update the content of an excel with strings.
	public void updateExcel (String excelName, int sheetnumber, String sheetName, int column, int line, String content){
		try {
			 Workbook wb  =  Workbook.getWorkbook( new  File( excelName + ".xls" ));
			 WritableWorkbook book  =  Workbook.createWorkbook( new  File( excelName + ".xls" ), wb);
			 WritableSheet sheet  =  book.createSheet( sheetName ,  sheetnumber - 1 );
			 sheet.addCell( new  Label( column - 1 ,  line - 1 ,  content ));
	         book.write();
	         book.close();
		} catch (Exception e){
			System.out.println(e);
		}
	}
	
	// The method to update the content of an excel with number.
	public void updateExcel (String excelName, int sheetnumber, String sheetName, int column, int line, double content){
		try {
			 Workbook wb  =  Workbook.getWorkbook( new  File( excelName + ".xls" ));
			 WritableWorkbook book  =  Workbook.createWorkbook( new  File( excelName + ".xls" ), wb);
			 WritableSheet sheet  =  book.createSheet( sheetName ,  sheetnumber - 1 );
			 jxl.write.Number number  =   new  jxl.write.Number( column -1 ,  line -1 ,  content );
			 sheet.addCell(number);
			 book.write();
			 book.close();
		} catch (Exception e){
			System.out.println(e);
		}
	}
	
	
	
	/*
	 * Copy the file or folder to the destination.
	 * 
	 * Author：Jester
	 */
	public static boolean copy(String iFrom,String iTo) throws IOException
	{	
		File from=new File(iFrom);
		File to=new File(iTo);
	
		FileHandler.copy(from, to);
		return true;		
	}
	
	/*
	 * Delete the file or folder..
	 * 
	 * Author：Jester
	 */
	public static boolean delete(String iToDelete) throws IOException
	{	
		File toDelete=new File(iToDelete);
	
		FileHandler.delete(toDelete);
		return true;		
	}
}
